Using Unity Container
var c = new UnityContainer(); Example 1: // Register ZLogger as an implementor of ILogger c.RegisterType(); // Create a new instance of ZLogger. var logger = c.Resolve(); Example 2: // Register ZLogger as an implementor of ILogger. Resolve will return a singleton (container controlled lifetime) c.RegisterType( new ContainerControlledLifetimeManager()); // Get the singleton instance of ZLogger (creating it the first time) var logger = c.Resolve(); Example 3: // Regsiter an existing instance of an ILogger implementation. Resolve will always return this instance. c.RegisterInstance(existingLogger); // Get the single registered instance of ILogger. var logger = c.Resolve(); Example 4: // What if ZLogger's constructor requires that the caller pass in an instance of ZStorage class. // This is solved by registering ZStorage type into the container. c.RegisterType(); // The rest of the code is the same as Example 1: c.RegisterType(); var logger = c.Resolve(); Example 5: // What if ZLogger had a public property caller LogFlags which can be set by a client. // We can decorate the property in the logger class with the attribute Dependency Dependency public LogFlagManager LogFlags { get { return _logFlags; } set { _logFlags = value; } } Now, creating an instance of the ZLogger class automatically generates an instance of the LogFlagManager class and sets it as the value of the LogFlags property of the ZLogger class. ---- // Example of configuring from the container from a configuration file. //These examples use the App.Config/Web.Config file. // Example code reading the defaul container from the config file var section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity"); section.Containers.Default.Configure©; // To read from a specific container, change the last line to: section.Containers"containerOne".Configure©; // To create a hierarchy of nested containers var pc = new UnityContainer(); var cc = parentContainer.CreateChildContainer(); var section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity"); section.Containers"containerOne".GetConfigCommand().Configure(pc); section.Containers"nestedChildContainer".Configure(pc); // Using an external XML configuration file: ExeConfigurationFileMap map = new ExeConfigurationFileMap(); map.ExeConfigFilename = "MyConfig.config"; System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None); var section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity"); section.Containers"containerOne".Configure©; //Example: ... ... ... ... // Example of creating some aliases. This go into the section // Example of creating some containers. These usually go after the sectin RegisterType(); RegisterInstance(exitingObject); Resolve(); Resolve(); BuildUp(existingObject) container.RegisterType(); Database database = container.Resolve(); ;CustomDatabase class constructor requires ILogger. CustomDatabase doesn't have to be registered with the container ILogger logger = Container.Resolve(); ;Resolve creates a new ILogger each time, unless you want a singleton. ---- container.RegisterType( new ContainerControlledLifetimeManager() ); ; singleton. container.RegisterType( "SQL" ); container.RegisterType( "ORACLE" ); Database database = container.Resolve("SQL"); ;using aliases. IEnumerable databases = container.ResolveAll(); ; return two databases, one for SQL one for ORACLE container.RegisterInstance( new SqlDatabase() ); container.RegisterInstance( "Oracle", new OracleDatabase() ); ; automatically a singleton. Database database = container.Resolve(); ; returns the SqlDatabase instance -- the default. ---- container.RegisterType(); DB2Database existingDatabase = new DB2Database(); container.BuildUp(existingDatabase) container.RegisterInstance(existingDatabase) Database database = container.Resolve(); ;DB2Database has a public property Dependency on ILogger. ;BuildUp injects the dependencies UnityContainer container = new UnityContainer(); UnityConfigurationSection section = (UnityConfigurationSection) ConfigurationManager.GetSection("unity"); section.Containers.Default.GetConfigCommand().Configure(container) ICustomerDatabase datasource = container.Resolve(); ---- IUnityContainer = childContainer = parentContainer.CreateChildContainer(); parentContainer.RegisterType( new ContainerControlledLifetimeManager()); ILogger logger = childContainer.Resolve(); ;childContainer doesn't have ILogger, so it goes to the parent. ---- ;VirtualMethodInterceptor example ???? IUnityContainer container = new UnityContainer(); container.AddNewExtension(); container.RegisterType(); container.Configure(). SetInterceptorFor(new VirtualMethodInterceptor()); var logger = container.Resolve(); logger.Write("World."); public class Logger { public virtual void Write(string message) { Console.Write(message); } } Category:Cheat